// pages/stroll_around/stroll_around.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adddatalist:[],
    time:30,
    times:'00:00',
    hodeshowbox: false, 
    baoxiang_box:false,
    my_claim:null,
    curpage:1,
    hasmore:true,
    bx_:false,
    current:2
  },
  bu0(n){
    return n<10?'0'+n:''+n;
  },
  getDuration(second) {
    var days = Math.floor(second / 86400);
    var hours = Math.floor((second % 86400) / 3600);
    var minutes = Math.floor(((second % 86400) % 3600) / 60);
    var seconds = Math.floor(((second % 86400) % 3600) % 60);
    // var duration = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
    var duration = this.bu0(minutes) + ":" + this.bu0(seconds);

    this.setData({
      times: duration
    })
  },
  adddatalist(){
    let that = this;
    let  curpage =  that.data.curpage
    if(!that.data.hasmore)return ;
    wx.request({
      url: ApiUrl + 'index.php?act=goods&op=goods_list',
      data: {
        curpage:curpage++,
        page:5
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var list = res.data.datas.goods_list;
        var glist = that.data.adddatalist.concat(list)
        that.setData({
          adddatalist: glist,
          curpage:curpage,
          hasmore:res.data.hasmore
        });
      },
      fail: function (res) {
        //console.log(res.data)
      },
    }); 
  },
  //index.php?act=other_box&op=my_receive
  my_receive(){
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=other_box&op=my_receive',
      data: {
        act: 'other_box',
        op: 'my_receive',
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: function (result) {
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas;
            that.setData({
              current: ++that.data.current
            })
          }
        }
      }
    });
  },
  //index.php?act=other_box&op=my_claim
  /** 
   * 获取我的宝箱领取情况
   */
  my_claim() {
    let that = this;
    let k = wx.getStorageSync('key');
    
    wx.request({
      url: ApiUrl + '/index.php?act=other_box&op=my_claim',
      data: {
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: function (result) {
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas;
                   that.setData({
                      current:res.current
                    })
            if (res.current == 0) {
                that.setData({
                  my_claim:res
                });
                that.get_time_desc();
            } else if (res.current == 1){
                   that.setData({
                      bx_:true
                    })
            }
          }
        }
      }
    });
  },
  kaibaoxiang(){
    this.setData({
      hodeshowbox: true,
      baoxiang_box: false
    })
  },
  /** 宝箱倒计时
  */  
  get_time_desc:function(){
      var that = this
                that.getDuration(that.data.time);
                                that.data.timer = setInterval(function () {
                                  if(that.data.time<=1){
                                            clearInterval(that.data.timer)
                                            that.my_receive();
                                            that.setData({
                                              bx_:true
                                            })
                                  }
                                  that.data.time--;
                                  that.getDuration(that.data.time);
                                }, 1000)
  },
  /**
                    if (that.data.time == 1) {
                    clearInterval(that.data.timer);

                    that.setData({
                      hodeshowbox: true,
                      baoxiang_box: true,
                    })

                  }
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.adddatalist();
    that.my_claim();
  },
  hidebtn(){
    this.setData({
      bx_: false,
                baoxiang_box: true,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
var that = this;
    that.setData({
      adddatalist:[],
      hasmore:true,
      time:30,
      bx_:false,
      curpage:1
    })
    clearInterval(that.data.timer)
    that.onLoad(); 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.adddatalist();
    if(that.data.bx_){
        if((!that.data.hasmore || that.data.curpage>3) && that.data.current<2){
          that.my_receive();
              that.setData({
                bx_: true,
                baoxiang_box: false,
                current:++that.data.current
              })
        }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})